using System;
using HCore.IOC.Context.Stereotype;
using JetBrains.Annotations;

namespace Session
{
    [UsedImplicitly]
    [Component]
    public class SessionService
    {
        private bool isLoadFinishSessionData; // 是否已经加载完成Session数据

        // [Autowired] private GameDataService aGameDataService;
        // [Autowired] private PoolManager aPoolManager;
        private Session CurrentSession { get; set; }

        /// <summary>
        ///     开始一场新的游戏
        /// </summary>
        /// <param name="jigsawImagePath"></param>
        /// <param name="tileCount"></param>
        /// <param name="rotateTile"></param>
        /// <param name="debug"></param>
        /// <param name="tileSize"></param>
        public void StartNewGame(string jigsawImagePath, int tileSize, int tileCount, bool rotateTile, bool debug = false)
        {
            isLoadFinishSessionData = false;
            // var level = aGameDataService.GetData<LevelGameData>().GetLevelInfo(levelName);
            CurrentSession = Session.Create(jigsawImagePath, tileSize, tileCount, rotateTile, debug);

            // var towerGameData = aGameDataService.GetData<TowerGameData>();
            // var cellInfoGameData = aGameDataService.GetData<CellInfoGameData>(); // 获取格子信息
            // var enemyGameData = aGameDataService.GetData<EnemyGameData>();

            // 装载 Session
            // CurrentSession.Setup(cellInfoGameData, towerGameData, enemyGameData);

            // 线程池加载完毕后，触发数据加载完成事件
            // aPoolManager.AddAsyncLoadFinishEvent((p1, p2) =>
            // {
            //     if (p1 != p2) return;
            //
            //     isLoadFinishSessionData = true;
            //     OnStartNewGameEvent?.Invoke(CurrentSession);
            // });

            isLoadFinishSessionData = true;

            NewGameStartBeforeEvent?.Invoke(CurrentSession);
            NewGameStartPostEvent?.Invoke(CurrentSession);
        }

        #region NewGameStartBeforeEvent

        private event Action<ISession> NewGameStartBeforeEvent;

        /// <summary>
        ///     注册“新游戏开始前”事件
        ///     <para>如果已经加载完成，则会立即执行一次</para>
        ///     <para>通常给Mono用来初始化数据用，可以清空前一场游戏的数据，准备新的游戏数据，调整Session中的数据。然后，等待新的一场游戏开始。</para>
        /// </summary>
        /// <param name="event"></param>
        public void AddNewGameStartBeforeEvent(Action<ISession> @event)
        {
            NewGameStartBeforeEvent -= @event;
            NewGameStartBeforeEvent += @event;
            if (isLoadFinishSessionData) @event.Invoke(CurrentSession);
        }

        #endregion

        #region NewGameStartPostEvent

        private event Action<ISession> NewGameStartPostEvent;

        /// <summary>
        ///     注册“新游戏开始后”事件
        ///     <para>如果已经加载完成，则会立即执行一次</para>
        ///     <para>此时，Session 已经创建且加载完毕，数据已经清晰或刷新，可以开始一场全新的游戏了</para>
        /// </summary>
        /// <param name="event"></param>
        public void AddNewGameStartPostEvent(Action<ISession> @event)
        {
            NewGameStartPostEvent -= @event;
            NewGameStartPostEvent += @event;
            if (isLoadFinishSessionData) @event.Invoke(CurrentSession);
        }

        #endregion
    }
}